package com.treasure.hunt.mode

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.ToastUtils
import com.module.lemlin.base.BaseApplication
import com.module.lemlin.base.BaseViewModel
import com.module.lemlin.mode.ViewModelSubscribe
import com.module.lemlin.rxhttp.HttpServiceData
import com.module.lemlin.rxhttp.HttpServiceResponse
import com.module.lemlin.rxhttp.msg
import com.treasure.hunt.R
import com.treasure.hunt.api.*
import com.treasure.hunt.entity.*
import com.treasure.hunt.popup.DialogSignPopup
import io.reactivex.rxjava3.disposables.Disposable
import kotlinx.android.synthetic.main.popup_dialog_sign.view.*

open class TaskViewMode(application: Application) : AdViewMode(application) {

    val taskDayLiveData = MutableLiveData<HttpServiceResponse<TaskDayResponse>>()

    val taskSignLiveData = MutableLiveData<HttpServiceResponse<TaskSignResponse>>()
    val baseLiveData = MutableLiveData<HttpServiceResponse<HttpServiceData>>()
    val taskShareLiveData = MutableLiveData<HttpServiceResponse<HttpServiceData>>()
    val luckySignLiveData = MutableLiveData<HttpServiceResponse<LuckySignResponse>>()
    val luckyReceiveLiveData = MutableLiveData<HttpServiceResponse<LuckyReceiveResponse>>()

    val signPopup: DialogSignPopup by lazy {
        DialogSignPopup(context = BaseApplication.INSTANCE, onConfirm = {
            taskSignLiveData.value?.httpData?.data?.let { taskSign ->
                if (it.id == R.id.tvDialogSign) {
                    isRewardVerifyAdSign = true
                    val maps: MutableMap<String, Any?> = mutableMapOf(
                        "advertisement_type" to 5,
                        "luck_draw_type" to 6
                    )
                    videoType(maps)
                } else if (it.id == R.id.tvDialogDoubled) {
                    val list: MutableList<SignList> = taskSign.list
                    if (list.isNullOrEmpty())
                        return@let
                    val index: Int = taskSign.sign_num
                        ?: return@let
                    val maps: MutableMap<String, Any?> = mutableMapOf(
                        "advertisement_type" to 5,
                        "luck_draw_type" to 7,
                        "model_type" to 6,
                        "reward_id" to list[index].id
                    )
                    videoType(maps)
                }
            }
        })
    }

    fun taskDayList() {
        val disposable: Disposable =
            taskDayList(ViewModelSubscribe(taskDayLiveData))
        dispose.add(disposable)
    }

    fun taskSignList(isMain: Boolean = false) {
        val listener = object : ViewModelSubscribe<TaskSignResponse>(taskSignLiveData) {
            override fun onSuccess(resp: TaskSignResponse) {
                super.onSuccess(resp)
                resp.data.let {
                    if (isMain && it.is_sign == 1) {
                        return@let
                    }
                    signPopup.setFillData(it)
                        .showPopupWindow()
                }
            }
        }
        val disposable: Disposable = taskSignList(listener)
        dispose.add(disposable)
    }

    fun taskSign(signId: Int, videoLogId: String, isDouble: Int = 2) {
        val listener = object : ViewModelSubscribe<LuckySignResponse>(luckySignLiveData) {
            override fun onSuccess(resp: LuckySignResponse) {
                super.onSuccess(resp)
                ToastUtils.showShort(resp.msg)
                signPopup.dismiss()
            }

            override fun onFailure(throwable: Throwable) {
                super.onFailure(throwable)
                signPopup.dismiss()
                ToastUtils.showShort(throwable.msg)
            }
        }
        val disposable: Disposable =
            taskSign(signId, videoLogId, isDouble, listener)
        dispose.add(disposable)
    }


    fun taskFinish(taskId: Int) {
        val disposable: Disposable =
            taskFinish(taskId, ViewModelSubscribe(luckyReceiveLiveData))
        dispose.add(disposable)
    }

    fun taskShare(type: Int) {
        val disposable: Disposable =
            taskShare(type, ViewModelSubscribe(baseLiveData))
        dispose.add(disposable)
    }

    val userInfoLiveData = MutableLiveData<HttpServiceResponse<UserInfoResponse>>()

    fun userInfo() {
        val disposable: Disposable =
            com.treasure.hunt.api.userInfo(ViewModelSubscribe(userInfoLiveData))
        dispose.add(disposable)
    }


    val otherConfigLiveData = MutableLiveData<HttpServiceResponse<OtherConfigResponse>>()
    val otherImageLiveData = MutableLiveData<HttpServiceResponse<OtherImageResponse>>()

    fun otherGetConfig(name: String) {
        val disposable: Disposable =
            com.treasure.hunt.api.otherGetConfig(name, ViewModelSubscribe(otherConfigLiveData))
        dispose.add(disposable)
    }

    fun otherGetImage(name: String) {
        val disposable: Disposable =
            otherGetImage(name, ViewModelSubscribe(otherImageLiveData))
        dispose.add(disposable)
    }

    val otherUrlLiveData = MutableLiveData<HttpServiceResponse<OtherUrlResponse>>()
    fun otherShareImage(type: Int, card_id: Int = 0) {
        val disposable: Disposable =
            otherShareImage(
                type.toString(), card_id.toString(),
                ViewModelSubscribe(otherUrlLiveData)
            )
        dispose.add(disposable)
    }


    val otherAppLiveData = MutableLiveData<HttpServiceResponse<OtherAppResponse>>()
    fun otherGetVersion() {
        val disposable: Disposable =
            otherGetVersion(
                AppUtils.getAppVersionName(), ViewModelSubscribe(otherAppLiveData)
            )
        dispose.add(disposable)
    }
}